/* $Id: yuv.c,v 1.6 2008/04/04 08:12:21 pekberg Exp $
******************************************************************************

   Display-file: yuv writer

   Copyright (C) 2007 Andreas Beck [becka-ggi@bedatec.de]

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   THE AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
   IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************************
*/

#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <fcntl.h>

#include <ggi/internal/ggi-dl.h>
#include <ggi/display/file.h>
#include <ggi/internal/gg_replace.h>	/* for snprintf() */


#define YUV_INFO_STRING  "# Generated by display-file target of LibGGI\n"

void _ggi_file_yuv_write(struct ggi_visual *vis)
{
	ggi_file_priv *priv = FILE_PRIV(vis);

	unsigned char buf[200];

	int x, y;

	ggi_pixel last;
	ggi_color col;


	/* write file header info, if mode was reset */

	if (priv->mode_reset) {
		snprintf((char *)buf, sizeof(buf),
			"YUV4MPEG2 W%d H%d F25:1 Ip A1:1\n",
			LIBGGI_VIRTX(vis)&~1, LIBGGI_VIRTY(vis)&~1);
		_ggi_file_write_string(vis, (const unsigned char *)buf);
		priv->mode_reset=0;	/* ok, we have it. */
	}

	/* write frame header otherwise */
	_ggi_file_write_string(vis, (const unsigned char *)"FRAME\n");

	/* write out the pixels */

	_ggiGetPixelNC(vis, 0, 0, &last);
	last = ~last;	/* must be different from first pixel */

	/* This is quite inefficient, gettng pixels individually and 
	 * running the loop thrice. Moreover color is not nicely averaged.
	 * We should rather allocate enough space to store the 3 parts and
	 * push them out in a single call.
	 */
	for (y=0; y < (LIBGGI_VIRTY(vis)&~1); y++)
	for (x=0; x < (LIBGGI_VIRTX(vis)&~1); x++) {

		ggi_pixel pix;
		int Y;

		_ggiGetPixelNC(vis, x, y, &pix);

		if (pix != last) {
			_ggiUnmapPixel(vis, pix, &col);
		}

		Y=(299*col.r+587*col.g+114*col.b)/257000;
		_ggi_file_write_byte(vis, (unsigned)Y);

		last = pix;
	}
	for (y=0; y < LIBGGI_VIRTY(vis); y+=2)
	for (x=0; x < LIBGGI_VIRTX(vis); x+=2) {

		ggi_pixel pix;
		int xx,yy;
		int U;

		U=0;
		for(yy=0;yy<2;yy++) {
			for(xx=0;xx<2;xx++) {
				_ggiGetPixel(vis, x+xx, y+yy, &pix);

				if (pix != last) {
					_ggiUnmapPixel(vis, pix, &col);
					last = pix;
				}
				U+=(-1687*col.r-3313*col.g+5000*col.b);
			}
		}

		U=U/(4*2570000)+128;
		_ggi_file_write_byte(vis, (unsigned)U);
	}
	for (y=0; y < LIBGGI_VIRTY(vis); y+=2)
	for (x=0; x < LIBGGI_VIRTX(vis); x+=2) {

		ggi_pixel pix;
		int xx,yy;
		int V;

		V=0;
		for(yy=0;yy<2;yy++) {
			for(xx=0;xx<2;xx++) {
				_ggiGetPixel(vis, x+xx, y+yy, &pix);

				if (pix != last) {
					_ggiUnmapPixel(vis, pix, &col);
					last = pix;
				}
				V+=(5000*col.r-4187*col.g- 813*col.b);
			}
		}

		V=V/(4*2570000)+128;
		_ggi_file_write_byte(vis, (unsigned)V);
	}

	_ggi_file_flush(vis);
}
